package cn.wit.luohui.day09;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.File;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.junit.Test;

public class DemoKeywordsCount {
private static final String CONTENT="abstract 	continue	for	new	switch\r\n" + 
		"assert***	default	goto*	package	synchronized\r\n" + 
		"boolean	do	if	private	this\r\n" + 
		"break	double	implements	protected	throw\r\n" + 
		"byte	else	import	public	throws\r\n" + 
		"case	enum****	instanceof	return	transient\r\n" + 
		"catch	extends	int	short	try\r\n" + 
		"char	final	interface	static	void\r\n" + 
		"class	finally	long	strictfp**	volatile\r\n" + 
		"const*	float	native	super	while\r\n";

//伪代码
@Test
public void count() throws IOException {
//	String file="‪D:\\test.java";
	String file ="E:\\camocode\\campa\\code\\src\\cn\\wit\\luohui\\day09\\DemoCheckedException.java";
//读取某个文件的所有内容
	//List<String> lines=Files.readAllLines(Paths.get(file));
	List<String> lines=Files.readAllLines(Paths.get(file));
	System.out.println(1);
	
	//java所有的关键字
	Set<String>keywords=getKeywords();
	
//	// key: 关键字， value：个数统计
	Map<String, Integer> counts = new HashMap<>();
	
	//把内容里面的单词解析出来
	for(String line:lines) {
		String[] words=line.split("[\t.?;()<>]");// \t表示制表键
		System.out.println(Arrays.toString(words));
		// 遍历每一个单词
		// 如果是关键字，就放入一个Map集合，计数加一
		for(String word:words) {
			if(keywords.contains(word)) {//
				// 如果在map里面有了，就+1
				// 没有，就先放进去，计数设置为1
				if(counts.containsKey(word)) {//如果在map里有了
					Integer count=counts.get(word);
					counts.put(word, ++count);
				}else {
					counts.put(word, 1);
				}
			}
		}
	}
	//遍历map集合，打印关键字的统计信息
	for(Entry<String,Integer>entry:counts.entrySet()) {
		System.out.printf("%s \t %d \n", entry.getKey(), entry.getValue());
	}
}
@Test
//拆分关键字
public void test() {
	String[] split=CONTENT.split("[\t*]");
	for(int i=0;i<split.length;i++) {
		System.out.println(i);
		System.out.println(split[i]);
	}
}

public static Set<String> getKeywords() {
	Set<String>result=new HashSet<>();//定义set集合，自动去重
	String[] split=CONTENT.split("[\t*]");
	for (int i=0;i<split.length;i++) {
		if("".equals(split[i])==false) {//若是空的字符串就去掉
			result.add(split[i]);
		}
	}
	return result;
}
}
